package com.gxuwz.userprovider.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxuwz.commonmodule.entity.User;
import com.gxuwz.userprovider.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;

    /**
     * 通过id查询用户信息
     * @param username
     * @return
     */
    @GetMapping("/findById/{username}")
    public User getUser(@PathVariable("username") String username){
        return userMapper.findByID(username);
    }

    /**
     * 查询所有用户信息
     * @param user
     * @return
     */
    @GetMapping("/list")
    public List<User> list(@RequestBody User user){
        return userMapper.query(user);
    }

    /**
     * 用户个人信息更新
     * @param username
     */
    @GetMapping("/{username}")
    public User findInfo(@PathVariable("username") String username){
        return userMapper.findInfo(username);
    }

    /**
     * 添加用户信息
     * @param user
     */
    @PostMapping("/add")
    public void addUser(@RequestBody User user){
        userMapper.insert(user);
    }

    /**
     * 通过username删除用户信息
     * @param username
     */
    @DeleteMapping("/delete/{username}")
    public void deleteUser(@PathVariable("username") String username){
        userMapper.delete(username);
    }

    /**
     * 更新用户信息
     * @param user
     */
    @PutMapping("/update")
    public void editUser(@RequestBody User user){
        userMapper.update(user);
    }

    /**
     * 分页（不带条件）
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/findPage")
    public PageInfo findPage(@RequestParam(value = "pageIndex") int pageIndex,@RequestParam(value = "pageSize") int pageSize){
        PageHelper.startPage(pageIndex,pageSize);
        List<User> users = userMapper.findPage(null);
        PageInfo<User> page = new PageInfo<>(users);
        return page;
    }
}
